# Friday, 13.October.2006

# the following is made to make ropeladders for the decade Poisson analysis;
# those for yearly analysis can be made in a similar way. 

dd<-c("C:/XunCao/Research/W_H_backup/MCMCoutputs/MID/dpt/Poisson")
setwd(dd)
Year<-c("195059", "196069", "197079", "198089", "199000") 
Years<-c("1950-1959", "1960-1969", "1970-1979", "1980-1989", "1990-2000") 

POL<-M<-IGO<-Dist<-POLS<-POLR<-NULL
for (i in 1:5){
burn<-c(rep(500,5))
burn<-burn[i]

out<-read.table (paste(Year[i], "/OUT", sep=""), header=T)

Sum.post<-NULL
for (j in c(8, 4:7, 9:dim(out)[2], 3)){
                               sum.post<-c(quantile(out[,j][-c(1:burn)], c(.025, .975))[1], 
                               mean(out[,j][-c(1:burn)]), 
                               quantile(out[,j][-c(1:burn)], c(.025, .975))[2], 
                               sd(out[,j][-c(1:burn)]))
                               Sum.post<-rbind(Sum.post, sum.post)
                               }

Sum.post<-round(Sum.post, digits=4)
colnames(Sum.post)<-c("2.5%", "Mean", "97.5%", "St.Dev")
rownames(Sum.post)<-colnames(out)[c(8, 4:7, 9:dim(out)[2], 3)]
# Sum.post

POL<-rbind(POL, Sum.post[2,1:3]); M<-rbind(M, Sum.post[3,1:3])
IGO<-rbind(IGO, Sum.post[4,1:3]); Dist<-rbind(Dist, Sum.post[5,1:3])
POLS<-rbind(POLS, Sum.post[8,1:3]); POLR<-rbind(POLR, Sum.post[11,1:3])
}

wd<-c("C:/XunCao/Temp/Temp/tile_AS/Tile")
setwd(wd)




library(MASS)
source("tile.r")

# POLS
add<-.05*abs(min(POLS[,1])-max(POLS[,3]))
at <- sort(c(0,round(seq(min(POLS[,1])-add, max(POLS[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m1<-POLS[,2]
lb1<-POLS[,1]
ub1<-POLS[,3]

postscript("PolInter_p_s.ps",height=3,width=3,horizontal=F,family="Times")
ropeladder (
x=cbind(m1),
l=cbind(lb1),
u=cbind(ub1),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("Initiator Democracy")),
xaxis=list(at=at), 
taxis=list(at=at), 
pch=pch,
gridlines=list(style="x"), 
shadebox=NULL,
RxC=c(1,1)
)
dev.off()


# POLR
add<-.05*abs(min(POLR[,1])-max(POLR[,3]))
at <- sort(c(0,round(seq(min(POLR[,1])-add, max(POLR[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m1<-POLR[,2]
lb1<-POLR[,1]
ub1<-POLR[,3]

postscript("PolInter_p_r.ps",height=3,width=3,horizontal=F,family="Times")
ropeladder (
x=cbind(m1),
l=cbind(lb1),
u=cbind(ub1),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("Target Democracy")),
xaxis=list(at=at), 
taxis=list(at=at), 
pch=pch,
gridlines=list(style="x"), 
shadebox=NULL,
RxC=c(1,1)
)
dev.off()



# POL
add<-.05*abs(min(POL[,1])-max(POL[,3]))
at <- sort(c(0,round(seq(min(POL[,1])-add, max(POL[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m1<-POL[,2];m2<-rep(.6, 5)
lb1<-POL[,1];lb2<-rep(.4, 5)
ub1<-POL[,3]; ub2<-rep(.8, 5)

postscript("PolInter_p.ps",height=3,width=3,horizontal=F,family="Times")
ropeladder (
x=cbind(m1),
l=cbind(lb1),
u=cbind(ub1),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("Democracy: i x j")),#"P(Never w/o Medicine)")),
#otitles=list(fontface="bold",labels=c("Figure 5: Effective Governance and Social Welfare")),
xaxis=list(at=at), 
# X-axis controls
taxis=list(at=at), # Top-axis controls
#usr=c(min(at),max(at)),
#output = list(wide=10,outfile=outfile, type="pdf"),
# Output file information
pch=pch, # Plotting symbol
gridlines=list(style="x"), 
shadebox=NULL,
# Add gray shading of point estimate range
RxC=c(1,1)
# Number of Rows and Columns of plots to make
)
dev.off()


# Imports:
postscript("M_p.eps",height=3,width=3,horizontal=F,family="Times")
M.def<-M

M<-M.def
M[1:2,]<-M[1:2,]/24; M[3,]<-M[3,]/2;
add<-.012*abs(min(M[,1])-max(M[,3]))
at <- sort(c(0,round(seq(min(M[,1])-add, max(M[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m2<-M[,2]
lb2<-M[,1]
ub2<-M[,3]

ropeladder (
x=cbind(m2),
l=cbind(lb2),
u=cbind(ub2),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("Imports")),#"P(Never w/o Medicine)")),
#otitles=list(fontface="bold",labels=c("Figure 5: Effective Governance and Social Welfare")),
xaxis=list(at=at), 
# X-axis controls
taxis=list(at=at), # Top-axis controls
#usr=c(min(at),max(at)),
#output = list(wide=10,outfile=outfile, type="pdf"),
# Output file information
pch=pch, # Plotting symbol
gridlines=list(style="x"), 
shadebox=NULL,
# Add gray shading of point estimate range
RxC=c(1,1)
# Number of Rows and Columns of plots to make
)
dev.off()


# IGO
postscript("IGO_p.ps",height=3,width=3,horizontal=F,family="Times")

add<-.05*abs(min(IGO[,1])-max(IGO[,3]))
at <- sort(c(0,round(seq(min(IGO[,1])-add, max(IGO[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m1<-IGO[,2];m2<-rep(.6, 11)
lb1<-IGO[,1];lb2<-rep(.4, 11)
ub1<-IGO[,3]; ub2<-rep(.8, 11)

ropeladder (
x=cbind(m1),
l=cbind(lb1),
u=cbind(ub1),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("IGO")),#"P(Never w/o Medicine)")),
#otitles=list(fontface="bold",labels=c("Figure 5: Effective Governance and Social Welfare")),
xaxis=list(at=at), 
# X-axis controls
taxis=list(at=at), # Top-axis controls
#usr=c(min(at),max(at)),
#output = list(wide=10,outfile=outfile, type="pdf"),
# Output file information
pch=pch, # Plotting symbol
gridlines=list(style="x"), 
shadebox=NULL,
# Add gray shading of point estimate range
RxC=c(1,1)
# Number of Rows and Columns of plots to make
)
dev.off()


# Dist
postscript("Dist_p.ps",height=3,width=3,horizontal=F,family="Times")

add<-.05*abs(min(Dist[,1])-max(Dist[,3]))
at <- sort(c(round(seq(min(Dist[,1])-add, max(Dist[,3])+add, length=2), digits=2)))
pch <- c(19,19,19, 19, 19)
labels <- Years
m1<-Dist[,2];m2<-rep(.6, 11)
lb1<-Dist[,1];lb2<-rep(.4, 11)
ub1<-Dist[,3]; ub2<-rep(.8, 11)

ropeladder (
x=cbind(m1),
l=cbind(lb1),
u=cbind(ub1),  
labels=labels, 
ptitles=list(fontface="bold",labels=c("Distance")),#"P(Never w/o Medicine)")),
#otitles=list(fontface="bold",labels=c("Figure 5: Effective Governance and Social Welfare")),
xaxis=list(at=at), 
# X-axis controls
taxis=list(at=at), # Top-axis controls
#usr=c(min(at),max(at)),
#output = list(wide=10,outfile=outfile, type="pdf"),
# Output file information
pch=pch, # Plotting symbol
gridlines=list(style="x"), 
shadebox=NULL,
# Add gray shading of point estimate range
RxC=c(1,1)
# Number of Rows and Columns of plots to make
)
dev.off()
